﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XSTDZ.ERP.Core.Extentions;
using XSTDZ.ERP.Core.Generator;

namespace XSTDZ.ERP.Domain.Models
{
    /// <summary>
    /// 出库单
    /// </summary>
    public partial class ProductSellBill
    {
        public ProductSellBill()
        {
            this.ProductSellBillDetails = new HashSet<ProductSellBillDetail>();
        }
        public ProductSellBill Init()
        {
            if (this.Id.Equals(Guid.Empty))
            {
                this.Id = (Guid)new SequentialIdentityGenerator().Next;
            }
            this.CreateTime = DateTime.Now;
            this.Deleted = 0;
            return this;
        }
        /// <summary>
        /// 添加出库单明细行
        /// </summary>
        /// <param name="productInBillDetail"></param>
        /// <returns></returns>
        public ProductSellBill AddChild(ProductSellBillDetail productOutBillDetail)
        {
            if (!this.ProductSellBillDetails.Contains(productOutBillDetail))
            {
                productOutBillDetail.ProductSellBillID = this.Id;
                this.ProductSellBillDetails.Add(productOutBillDetail.Init());//进行初始化
            }
            return this;
        }
        public ProductSellBill AddChildren(IEnumerable<ProductSellBillDetail> productOutBillDetails)
        {
            if (productOutBillDetails != null && productOutBillDetails.Count() != 0)
            {
                productOutBillDetails.Each(p => AddChild(p));
            }
            return this;
        }
        public ProductSellBill ClearChildrenAll()
        {
            if (!this.ProductSellBillDetails.Equals(null) && this.ProductSellBillDetails.Count() != 0)
            {
                this.ProductSellBillDetails.Clear();
            }
            return this;
        }
        /// <summary>
        /// 设置删除状态（若为删除，则置为正常，否则为删除）
        /// </summary>
        /// <returns></returns>
        public ProductSellBill SetDeleteState()
        {
            this.Deleted = this.Deleted == 0 ? 1 : 0;
            return this;
        }
        /// <summary>
        ///  设置删除状态（若为删除，则置为正常，否则为删除）
        /// </summary>
        /// <param name="productInBillDetails"></param>
        /// <returns></returns>
        public ProductSellBill SetChildrenDeleteState(IEnumerable<ProductSellBillDetail> productOutBillDetails)
        {
            if (!productOutBillDetails.Equals(null) && productOutBillDetails.Count() != 0)
            {
                ProductSellBillDetails.Each(p => p.SetDeleteState());
            }
            return this;
        }

    }
}
